﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using EnnTrading.DataAccess.DBUtility;
using System.Data;

namespace MyWCF.Demo.Client.Web.Code
{
    public class ChatRoomUsersDB
    {
        private const string PARAM_ID = "@Id";
        private const string PARAM_CHAT_ROOM_ID = "@ChatRoomId";
        private const string PARAM_USER_ID = "@UserId";
        private const string PARAM_USER_NAME = "@UserName";
        private const string PARAM_READ_ONLY = "@ReadOnly";
        private const string PARAM_START = "@Start";
        private const string PARAM_COUNT = "@Count";

        private static  string ConnectionString = SQLHelper.CONN_STRING;

        private static void FillEntityFrom(SqlDataReader reader, ChatRoomUsersEntity obj)
        {
            #region
            if (reader != null && !reader.IsClosed)
            {
                obj.Id = reader.IsDBNull(reader.GetOrdinal("Id")) ? 0 : reader.GetInt64(reader.GetOrdinal("Id"));
                obj.ChatRoomId = reader.IsDBNull(reader.GetOrdinal("ChatRoomId")) ? 0 : reader.GetInt64(reader.GetOrdinal("ChatRoomId"));
                obj.UserId = reader.IsDBNull(reader.GetOrdinal("UserId")) ? 0 : reader.GetInt64(reader.GetOrdinal("UserId"));
                obj.UserName = reader.IsDBNull(reader.GetOrdinal("UserName")) ? String.Empty : reader.GetString(reader.GetOrdinal("UserName"));
                obj.ReadOnly = reader.IsDBNull(reader.GetOrdinal("ReadOnly")) ? Convert.ToByte(0) : reader.GetByte(reader.GetOrdinal("ReadOnly"));
            }
            #endregion
        }

        public static bool Add(ChatRoomUsersEntity obj)
        {
            SqlParameter[] dbParams ={
							 SQLHelper.MakeParam(PARAM_CHAT_ROOM_ID, SqlDbType.BigInt,0,obj.ChatRoomId),
							 SQLHelper.MakeParam(PARAM_USER_ID, SqlDbType.BigInt,0,obj.UserId),
							 SQLHelper.MakeParam(PARAM_USER_NAME, SqlDbType.NVarChar, 50,obj.UserName),					
							 SQLHelper.MakeParam(PARAM_READ_ONLY, SqlDbType.TinyInt,0,obj.ReadOnly)					
					};
            var result = SQLHelper.ExecuteScalar(ConnectionString, CommandType.StoredProcedure, "ChatRoomUsers_Add", dbParams);
            if (result != DBNull.Value)
            {
                obj.Id = long.Parse(result.ToString());
                return true;
            }
            return false;
        }

        public static List<ChatRoomUsersEntity> GetListByChatRoomId(long chatRoomId)
        {
            SqlDataReader reader = null;
            List<ChatRoomUsersEntity> result = new List<ChatRoomUsersEntity>();
            try
            {
                SqlParameter[] dbParams ={
							 SQLHelper.MakeParam(PARAM_CHAT_ROOM_ID, SqlDbType.BigInt,0,chatRoomId)				
					};
                reader = SQLHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "ChatRoomUsers_GetListByChatRoomId", dbParams);
                while (reader.Read())
                {
                    ChatRoomUsersEntity entity = new ChatRoomUsersEntity();
                    FillEntityFrom(reader, entity);
                    result.Add(entity);
                }
            }
            finally
            {
                if (reader != null && reader.IsClosed == false)
                {
                    reader.Close();
                }
            }
            return result;
        }
    }
}
